use glium::glutin::window::WindowBuilder;
use glium::{
    glutin::{
        event::{Event, WindowEvent},
        event_loop::{ControlFlow, EventLoop},
    },
};
use toy_brick::backend_glium::GLProjector;
use toy_brick::{Camera, IProjector, Space};
use zmath::Vec4;

fn main() {
    let el = EventLoop::new();
    let wb = WindowBuilder::new().with_title("Hello ToyBrick");
    let mut projector = GLProjector::new(wb, &el);

    let space = create_space();
    projector.set_scene(&space);

    let camera = Camera::new();

    el.run(move |event, _, control_flow| {
        *control_flow = ControlFlow::Poll;

        match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => {
                println!("The close button was pressed; stopping");
                *control_flow = ControlFlow::Exit
            }
            Event::MainEventsCleared => {
                projector.display().gl_window().window().request_redraw();
            }
            Event::RedrawRequested(_) => {
                projector.play_film(&camera);
            }
            _ => (),
        }
    })
}

fn create_space() -> Space {
    let mut space = Space::new();
    space.set_background_color(Vec4(0.0, 0.0, 0.0, 1.0));

    space
}
